#ifndef __PLATEAU_CC__
#define __PLATEAU_CC__

#include "grille.cc"
#include "plateau.hh"

Plateau::Plateau () : player(true),
	grille(PLATEAU_GRILLE_WIDTH, PLATEAU_GRILLE_HEIGHT,
		PLATEAU_CASE_VIDE),
	territory(PLATEAU_GRILLE_WIDTH, PLATEAU_GRILLE_HEIGHT,
		NULL)
{
	this->numb[0] = 0;
	this->numb[1] = 0;
	this->eval[0] = 0;
	this->eval[1] = 0;
}

void Plateau::placer (const Point &pt, const Player &pl) {
	this->grille.valeur(pt) = valPlayer(pl);
}

void Plateau::jouer (const Coup &c) {
	if (c.longueur() == 4) {
		this->deplacer(c.getOrig(), c.getDest());
	} else {
		this->placer(c.getDest(), this->player);
	}
	
	contaminer(c.getDest(), this->player);
	
	this->swap();
}

void Plateau::deplacer (const Point &o, const Point &d) {
	this->grille.valeur(d) = this->grille.valeur(o);
	this->grille.valeur(o) = PLATEAU_CASE_VIDE;
}

void Plateau::contaminer (const Point &p, const Player &pl) {
	int x, y;
	Player other = !pl;
	
	for (x = p.x - 1; x <= p.x + 1; x++)
	for (y = p.y - 1; y <= p.y + 1; y++) {
		if (this->grille.valide(x, y)) {
			if (this->grille.valeur(x, y) == valPlayer(other)) {
				this->grille.valeur(x, y) = valPlayer(pl);
				this->setNumbPlayer(pl, 1);
				this->setNumbPlayer(other, -1);
			}
			/*
			else if (this->grille.valeur(x, y) == PLATEAU_CASE_VIDE) {
				this->grille.valeur(x, y) = valPlayer(pl);
				this->setNumbPlayer(pl, 1);
			}*/
		}
	}
}

void Plateau::occuperTerritoire (const Point &p, const Player &pl) {
	int x, y;
	Player other = !pl;
	/*
	for (x = p.x - 1; x <= p.x + 1; x++)
	for (y = p.y - 1; y <= p.y + 1; y++) {
		if (this->grille.valide(x, y)) {
			if (this->territory.valeur(x, y) == PLATEAU_CASE_VIDE
					&& this->grille.valeur(x, y)) {
				this->territory.valeur(x, y) = valPlayer(pl);
				this->setEvalPlayer(pl, 1);
			}
		}
	}
	*/
}

void Plateau::swap () {
	this->player = !this->player;
}

void Plateau::setNumbPlayer (const Player &p, int value) {
	numb[valPlayer(p) - 1] += value;
}

void Plateau::setEvalPlayer (const Player &p, int value) {
	eval[valPlayer(p) - 1] += value;
}

ostream &operator << (ostream &os, const Plateau &pl)
{
	cout << "P1 : " << pl.numb[0]
		<< " - P2 : " << pl.numb[1] << endl << endl;
	cout << "Grille :" << endl;
	cout << pl.grille << endl;
	//cout << "Masque territoire :" << endl;
	//cout << pl.territory << endl;
	
	return os;
}

ostream &operator << (ostream &os, const Grille<int> &gr)
{
	os << "  ";
	for (int i = 0; i < gr.width; i++) {
		os << char(i + 'A');
	}
	os << endl;
	
	for (int y = 0; y < gr.height; y++) {
		os << y + 1 << " ";
		for (int x = 0; x < gr.width; x++) {
			switch (gr.valeur (x, y)) {
			case PLATEAU_CASE_VIDE :
				os << "_";
				break;
			case P1_ID :
				os << "1";
				break;
			case P2_ID :
				os << "2";
				break;
			case PLATEAU_TROU :
				os << "O";
				break;
			}
		}
		os << endl;
	}
	
	return os;
}

ostream &operator << (ostream &os, const Grille<int*> &gr)
{
	for (int y = 0; y < gr.height; y++) {
		for (int x = 0; x < gr.width; x++) {
			if (gr.valeur (x, y) == NULL)
				os << "0";
			else
				os << *gr.valeur (x, y);
		}
		os << endl;
	}
	
	return os;
}

#endif
